
const userList = [
    {
        id:1,
        name:'zhangshan',
        auth:[2,3,6,7],
        right:['edit','view']
    },{
        id:2,
        name:'lisi',
        auth:[2,3,5,6,7,8],
        right:['view']
    },{
        id:3,
        name:'wangwu',
        auth:[2,3,4,5,6,7,8],
        right:['edit','view','delete']
    }
]
type User = typeof userList[number]
interface Item {
    [p:string]:any
}
// type Ditem<T>= {
//     [prop in keyof T]: T[prop]
// }
// type Da = Ditem<DataItem>
const mydata:Item[] = [
    {
            id:2,
            pid:0,
            path:'/course',
            link:'/course',
            name:'Course',
            title:'课程管理',
        },{
            id:3,
            pid:2,
            path:'operate',
            name:'CourseOperate',
            link:'/course/operate',
            title:'课程操作'
        },
        {
            id:4,
            pid:3,
            path:'info_data',
            name:'CourseInfoData',
            link:'/course/operate/info_data',
            title:'课程信息'
        },{
            id:5,
            pid:2,
            path:'add',
            name:'CourseAdd',
            link:'/course/add',
            title:'课程增加'
        },{
            id:6,
            pid:0,
            path:'/student',
            name:'Student',
            link:'/student',
            title:'学生管理'
        },{
            id:7,
            pid:6,
            path:'operate',
            name:'StudentOperate',
            link:'/student/operate',
            title: '课程操作'
        },{
            id:8,
            pid:6,
            path: 'add',
            name: 'StudentAdd',
            link: '/student/add',
            title: '增加学生'
    
        }
    ]
    const myname = 'wangwu';
  function getDataList(userList:User[],myname:string):Item[]{
    const user = userList.find(m=>m.name===myname);
    const authList:Item[] = [];
    if(user){
        user.auth.map(a=>{
            mydata.map((r:Item)=>{
                if(a===r.id){
                    r.right = user.right;
                    authList.push(r)
                }
            })
        })
    }
return authList
  }

  //  JSON 做个深拷贝，避免源数据 result 被修改
  var result = getDataList(userList,myname)
 var result1:Item[] = JSON.parse(JSON.stringify(result))

 //DataTree  路由和菜单数据混在一起 需要分离
 var DataTree = result1.reduce((pre,next,index,arr)=>{
    var arr1 = arr.filter(n=>n.pid ===next.id);
    if(arr1.length){
        next.children = arr1
    }
    if(next.pid===0){
        pre.push(next)
    }
    return pre
; },[])

console.log(DataTree)
//  分离出路由树结构,  菜单一样   就不做菜单了
export var routeTreeData = DataTree.map(function fn(item:Item){
    let obj:any = {}
    if(item.children){
        obj.path=item.path;
        obj.name=item.name;
        obj.component = ()=>import(`../view/${item.name}.vue`)
        obj.children = item.children.map((item:Item)=>fn(item))
        return obj;
    }else{
        obj.path = item.path;
        obj.name=item.name;
        obj.component = ()=>import(`../view/${item.name}.vue`)
        return obj 
    }
})

export var menuTreeData = DataTree.map(function fn(item:Item){
    let obj:any = {}
    if(item.children){
        obj.path=item.path;
        // obj.name=item.name;
        obj.title = item.title;
        obj.link =item.link
        obj.children = item.children.map((item:Item)=>fn(item))
        return obj;
    }else{
        obj.path = item.path;
        // obj.name=item.name;
        obj.link=item.link;
        obj.title= item.title
        return obj 
    }
})


// Object.entries()
const myObj = {name:'李小凡',age:13,gender:'男'}
var res = Object.entries(myObj)
for(var [key,value] of res){
    console.log(key,value)
}
